package com.wlcj.www.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.nutz.ioc.loader.annotation.IocBean;

import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.wlcj.constant.Global;
import com.wlcj.util.Lang;
import com.wlcj.util.SqlUtil;
import com.wlcj.www.model.platform.HotPlatform;
import com.wlcj.www.model.platform.Platform;
import com.wlcj.www.model.platform.PlatformImg;
import com.wlcj.www.model.platform.PlatformPrice;
import com.wlcj.www.model.product.UnfinishedProduct;

@SuppressWarnings({"rawtypes", "unchecked"})
@IocBean
public class ProductService {
	
	/**
	 * 
	 * @param level 平台星级
	 * @param year	年华收益
	 * @param date	投资期限
	 * @param pid	平台id
	 * @param pageNo
	 * @return
	 */
	
	public Page<UnfinishedProduct> getUnfinishedProductPage(Integer level, Integer year, Integer date, Integer id, Integer orderby, String keywords, int pageNo) {
		String sql = "from wd_p_product_unfinished where 1=1 ";
		List paramList = Lists.newArrayList();
		UnfinishedProduct ups =  UnfinishedProduct.dao.findFirst("select max(create_time) as date from wd_p_product_unfinished ");
		if(level!=null) {
			sql += " and level = ?";
			paramList.add(level);
		}
		if(year!=null) {
			/**
			 *  <a href="javascript:;" target="_blank" value="1">20%-16%</a>
				<a href="javascript:;" target="_blank" value="2">16%-12%</a>
				<a href="javascript:;" target="_blank" value="3">12%-8%</a>
				<a href="javascript:;" target="_blank" value="4">8%以下</a>
			 */
			switch(year) {
			case 1:sql += " and level >= 0.16 and level < 0.2 ";break;
			case 2:sql += " and level >= 0.12 and level < 0.16 ";break;
			case 3:sql += " and level >= 0.08 and level < 0.12 ";break;
			case 4:sql += " and level < 0.08 ";break;
			}
		}
		if(date!=null) {
			/**
			 *  <a href="javascript:;" target="_blank" value="1">30天以内</a>
				<a href="javascript:;" target="_blank" value="2">1-3个月</a>
				<a href="javascript:;" target="_blank" value="3">3-6个月</a>
				<a href="javascript:;" target="_blank" value="4">6-12个月</a>
				<a href="javascript:;" target="_blank" value="5">12个月以上</a>
			 */
			switch(year) {
			case 1:sql += " and borrow_date < 30 ";break;
			case 2:sql += " and borrow_date >= 30 and borrow_date < 90 ";break;
			case 3:sql += " and borrow_date >= 90 and borrow_date < 180 ";break;
			case 4:sql += " and borrow_date >= 180 and borrow_date < 360 ";break;
			case 5:sql += " and borrow_date >= 360 ";break;
			}
			sql += " and level = ?";
			paramList.add(level);
		}
		if(id!=null) {
			sql += " and platform_id = ?";
			paramList.add(id);
		}
		if(StringUtils.isNotBlank(keywords)) {
			sql += " and title like ?";
			paramList.add("%"+keywords+"%");
		}
		sql += " and create_time = ?";
		paramList.add(ups.get("date"));
		if(orderby!=null) {
			switch(orderby){
			case 1:sql += " order by borrow_date desc";break;
			case 2:sql += " order by year_rate desc";break;
			case 3:sql += " order by borrow_amount desc";break;
			}
		} else {
			sql += " order by create_time desc";
		}
		Page<UnfinishedProduct> page = UnfinishedProduct.dao.paginate(pageNo, Global.PAGESIZE, "select * "
				, sql, paramList.toArray());
		for(UnfinishedProduct up:page.getList()) {
			// 每万元收益改为自动计算 天数*10000*年化率/365  一个月30天
			int day = up.getInt("borrow_date");
			BigDecimal yearRate = up.getBigDecimal("year_rate").divide(new BigDecimal(100));
			BigDecimal perIncome = new BigDecimal(day).multiply(new BigDecimal(10000)).multiply(yearRate)
					.divide(new BigDecimal(365), 2, RoundingMode.HALF_UP);
			up.put("per_income", perIncome);
			up.put("borrow_amount_display", Lang.formatMoney(up.getBigDecimal("borrow_amount")));
			up.put("year_rate_display", Lang.formatMoney(up.getBigDecimal("year_rate")));
			int pid = up.getInt("platform_id");
			System.out.println(pid);
			PlatformImg pi = PlatformImg.dao.getCachePlatformImgByPid(pid);
			up.put("img", pi.getStr("logo"));
		}
		return page;
	}
	
	public Page<UnfinishedProduct> getUnfinishedProductPage(Integer[] remen,Integer[] anquan,Integer[] touzi,Integer[] plat,Integer[] nianhua,String paixu,int pageNo) {
		String sql = "from wd_p_product_unfinished where 1=1 ";
		List paramList = Lists.newArrayList();
		UnfinishedProduct ups =  UnfinishedProduct.dao.findFirst("select max(create_time) as date from wd_p_product_unfinished ");
		if(remen!=null){//热门标签
			String a = "and ( ";
			for(int i=0;i<remen.length;i++){
				if(i==0){
					if(remen[i]==1){//12-14% 14-16% 稳健性
						a += "  year_rate > 12 and year_rate <16   ";
					}else if(remen[i]==2){//16%以上 高收益
						a += "  year_rate >= 16 ";
					}else if(remen[i]==3){//8%以下 8-12%以上低风险
						a += "  year_rate <= 12 ";
					}
				}else{
					if(remen[i]==1){//12-14% 14-16% 稳健性
						a += " or 12 =< year_rate <=16   ";
					}else if(remen[i]==2){//16%以上 高收益
						a += " or year_rate >= 16 ";
					}else if(remen[i]==3){//8%以下 8-12%以上低风险
						a += " or year_rate <= 12 ";
					}
				}
			}
			a+=" ) ";
			sql += a ;
		}
		if(anquan!=null){//安全评级
			String a = " ";
			for(int i=0;i<anquan.length;i++){
				a+= anquan[i];
				if(i!=anquan.length-1){
					a+=",";
				}
			}
			sql += " and level in("+a+")";
		}
		if(touzi!=null){//投资期限
			String a = " and ( ";
			for(int i=0;i<touzi.length;i++){
				if(i==0){
					if(touzi[i]==1){//一月一下
						a+="  borrow_date <= 30";
					}else if(touzi[i]==2){//1-3
						a += "   ( 30 < borrow_date and borrow_date <= 90 )  ";
					}else if(touzi[i]==3){//3-6
						a += "   ( 90 < borrow_date and borrow_date <= 180 )  ";
					}else if(touzi[i]==4){//6-12
						a += "  ( 180 < borrow_date and borrow_date <= 360 ) ";
					}else if(touzi[i]==5){//12--
						a += "  borrow_date > 360 ";
					}
				}else{
					if(touzi[i]==1){//一月一下
						a+=" or borrow_date <= 30";
					}else if(touzi[i]==2){//1-3
						a += "  or ( 30 < borrow_date and borrow_date <= 90 )  ";
					}else if(touzi[i]==3){//3-6
						a += "  or ( 90 < borrow_date and borrow_date <= 180 )  ";
					}else if(touzi[i]==4){//6-12
						a += "  or ( 180 < borrow_date and borrow_date <= 360 ) ";
					}else if(touzi[i]==5){//12--
						a += " or borrow_date > 360 ";
					}
				}
					
					
			}
			a +=" ) ";
			sql += a;
		}
		if(plat!=null){//所属平台
			
		}
		if(nianhua!=null&&remen==null){//年华收益
			String a = " and ( ";
			for(int i=0;i<nianhua.length;i++){
				if(i==0){
					if(nianhua[i]==1){//8--
						a += "  year_rate <= 8   ";
					}else if(nianhua[i]==2){//
						a += " (year_rate > 8 and year_rate <= 12 )";
					}else if(nianhua[i]==3){//14-16
						a += " (year_rate > 12 and year_rate <= 14 )";
					}else if(nianhua[i]==4){//14-16
						a += " (year_rate > 14 and year_rate <= 16 ) ";
					}else{
						a += " year_rate > 16 ";
					}
				}else{
					if(nianhua[i]==1){//8--
						a += "  year_rate <= 8   ";
					}else if(nianhua[i]==2){//
						a += " (year_rate > 8 and year_rate <= 12 )";
					}else if(nianhua[i]==3){//14-16
						a += " (year_rate > 12 and year_rate <= 14 )";
					}else if(nianhua[i]==4){//14-16
						a += " (year_rate > 14 and year_rate <= 16 ) ";
					}else{
						a += " year_rate > 16 ";
					}
				}
			}
			a+=" ) ";
			sql += a;
		}
		sql += " and create_time = ?";
		paramList.add(ups.get("date"));
		if(paixu!=null){//排序
			sql += " order by "+paixu+" desc";
		}else{
			sql += " order by create_time desc";
		}
		
		Page<UnfinishedProduct> page = UnfinishedProduct.dao.paginate(pageNo, Global.PAGESIZE, "select * "
				, sql ,paramList.toArray());
		for(UnfinishedProduct up:page.getList()) {
			// 每万元收益改为自动计算 天数*10000*年化率/365  一个月30天
			int day = up.getInt("borrow_date");
			BigDecimal yearRate = up.getBigDecimal("year_rate").divide(new BigDecimal(100));
			BigDecimal perIncome = new BigDecimal(day).multiply(new BigDecimal(10000)).multiply(yearRate)
					.divide(new BigDecimal(365), 2, RoundingMode.HALF_UP);
			up.put("per_income", perIncome);
			up.put("borrow_amount_display", Lang.formatMoney(up.getBigDecimal("borrow_amount")));
			up.put("year_rate_display", Lang.formatMoney(up.getBigDecimal("year_rate")));
			int pid = up.getInt("platform_id");
			PlatformImg pi = PlatformImg.dao.getCachePlatformImgByPid(pid);
			up.put("img", pi.getStr("logo"));
		}
		return page;
	}
	public List<HotPlatform> getHotPlatforms() {
		List<HotPlatform> list = HotPlatform.dao.find(SqlUtil.sql("platform.getHotPlatformIds"));
		for(HotPlatform hp:list) {
			int pid = hp.getInt("platform_id");
			Platform p = Platform.dao.getCachePlatformById(pid);
			hp.put("pname", p.getStr("platform_name"));
			PlatformPrice pp = PlatformPrice.dao.getCachePlatformPriceByPid(pid);
			hp.put("prate", Lang.formatMoney(pp.getBigDecimal("comprehensive_income")));
		}
		return list;
	}
	
	public List<Record> getPopProductTop10() {
		List<Record> list = Db.find(SqlUtil.sql("product.getPopPlatformTop10"));
		return list;
	}

	public Page<UnfinishedProduct> getPageByName(String name){
		String sql = "from wd_p_product_unfinished where 1=1 ";
		List paramList = Lists.newArrayList();
		UnfinishedProduct ups =  UnfinishedProduct.dao.findFirst("select max(create_time) as date from wd_p_product_unfinished ");
		if(StringUtils.isNotBlank(name)) {
			sql += " and title like ?";
			paramList.add("%"+name+"%");
		}
		sql += " and create_time = ?";
		paramList.add(ups.get("date"));
		Page<UnfinishedProduct> page = UnfinishedProduct.dao.paginate(1, Global.PAGESIZE, "select * "
				, sql ,paramList.toArray());
		for(UnfinishedProduct up:page.getList()) {
			// 每万元收益改为自动计算 天数*10000*年化率/365  一个月30天
			int day = up.getInt("borrow_date");
			BigDecimal yearRate = up.getBigDecimal("year_rate").divide(new BigDecimal(100));
			BigDecimal perIncome = new BigDecimal(day).multiply(new BigDecimal(10000)).multiply(yearRate)
					.divide(new BigDecimal(365), 2, RoundingMode.HALF_UP);
			up.put("per_income", perIncome);
			up.put("borrow_amount_display", Lang.formatMoney(up.getBigDecimal("borrow_amount")));
			up.put("year_rate_display", Lang.formatMoney(up.getBigDecimal("year_rate")));
			int pid = up.getInt("platform_id");
			PlatformImg pi = PlatformImg.dao.getCachePlatformImgByPid(pid);
			up.put("img", pi.getStr("logo"));
		}
		return page;
	}
	
	public Page<UnfinishedProduct> getPageByPlatName(int id){
		return null;
	}
}
